﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using JM.JCY.BusinessEntity;
using JM.JCY.Proxy;
using NCS.IConnect.ApplicationContexts;
using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using Microsoft.SharePoint;

namespace JM.JCY.SharePointComponent.IncorruptArchives.JianChaGanbuPeiOuCongYeDetails
{
    public partial class JianChaGanbuPeiOuCongYeDetailsUserControl : UserControl
    {
        public int _caseId
        {
            set { ViewState["_caseId"] = value; }
            get { return (int)ViewState["_caseId"]; }
        }

        public string _isApproval
        {
            set { ViewState["_isApproval"] = value; }
            get { return (string)ViewState["_isApproval"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                using (IncorruptProxy proxy = new IncorruptProxy())
                {
                    if (Request.QueryString["CaseId"] != null)
                    {
                        _caseId = Int32.Parse(Request.QueryString["CaseId"].ToString());
                        _isApproval = Request.QueryString["IsApproval"].ToString();

                        var loadDT = proxy.GetJianChaGanbuPeiOuCongYe(_caseId, string.Empty);
                        Session["EditJianChaGanbuPeiOuCongYeDT"] = loadDT;
                        LoadForView(loadDT);
                        LoadForEdit(loadDT);

                    }
                }

            }

            // switch controls
            if (this.hfIsEdit.Value != "1")
                SwitchControls(true, false);
            else
                SwitchControls(false, true);
        }

        private void LoadForEdit(IncorruptDataSet.T_JianChaGanbuPeiOuCongYeDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.txtFillerCompany.Text = loadRow.FillerCompany;
            this.txtFiller.Text = loadRow.Filler;
            this.rbGender.SelectedValue = loadRow.Gender;
            this.txtAge.Text = loadRow.Age;
            this.txtCompany.Text = loadRow.Company_Dept;
            this.txtPosition.Text = loadRow.Position;
            this.txtRank.Text = loadRow.Rank;

            // relation1
            this.txtRelatedName1.Text = loadRow.RelatedName1;
            this.txtRelationship1.Text = loadRow.Relationship1;
            this.rbRelatedGender1.SelectedValue = loadRow.RelatedGender1;
            this.txtRelatedAge1.Text = loadRow.RelatedAge1;
            this.txtRelatedCompany1.Text = loadRow.RelatedCompany1;
            this.txtRelatedPosition1.Text = loadRow.RelatedPosition1;
            //relation2
            this.txtRelatedName2.Text = loadRow.RelatedName2;
            this.txtRelationship2.Text = loadRow.Relationship2;
            this.rbRelatedGender2.SelectedValue = loadRow.RelatedGender2;
            this.txtRelatedAge2.Text = loadRow.RelatedAge2;
            this.txtRelatedCompany2.Text = loadRow.RelatedCompany2;
            this.txtRelatedPosition2.Text = loadRow.RelatedPosition2;
            //relation3
            this.txtRelatedName3.Text = loadRow.RelatedName3;
            this.txtRelationship3.Text = loadRow.Relationship3;
            this.rbRelatedGender3.SelectedValue = loadRow.RelatedGender3;
            this.txtRelatedAge3.Text = loadRow.RelatedAge3;
            this.txtRelatedCompany3.Text = loadRow.RelatedCompany3;
            this.txtRelatedPosition3.Text = loadRow.RelatedPosition3;

            this.txtApprovalComments.Text = loadRow.ApprovalComments;
            this.hfFillDate.Value = loadRow.FillDate.ToString();
        }

        private void LoadForView(IncorruptDataSet.T_JianChaGanbuPeiOuCongYeDataTable loadDT)
        {
            var loadRow = loadDT[0];

            this.lblFillerCompany.Text = loadRow.FillerCompany;
            this.lblFillDate.Text = loadRow.FillDate.ToString(JCYConstant.Date_Format_CN);
            this.lblFiller.Text = loadRow.Filler;
            this.lblGender.Text = loadRow.Gender;
            this.lblAge.Text = loadRow.Age;
            this.lblCompany.Text = loadRow.Company_Dept;
            this.lblPosition.Text = loadRow.Position;
            this.lblRank.Text = loadRow.Rank;

            // relation1
            this.lblRelatedName1.Text = loadRow.RelatedName1;
            this.lblRelationship1.Text = loadRow.Relationship1;
            this.lblRelatedGender1.Text = loadRow.RelatedGender1;
            this.lblRelatedAge1.Text = loadRow.RelatedAge1;
            this.lblRelatedCompany1.Text = loadRow.RelatedCompany1;
            this.lblRelatedPosition1.Text = loadRow.RelatedPosition1;
            //relation2
            this.lblRelatedName2.Text = loadRow.RelatedName2;
            this.lblRelationship2.Text = loadRow.Relationship2;
            this.lblRelatedGender2.Text = loadRow.RelatedGender2;
            this.lblRelatedAge2.Text = loadRow.RelatedAge2;
            this.lblRelatedCompany2.Text = loadRow.RelatedCompany2;
            this.lblRelatedPosition2.Text = loadRow.RelatedPosition2;
            //relation3
            this.lblRelatedName3.Text = loadRow.RelatedName3;
            this.lblRelationship3.Text = loadRow.Relationship3;
            this.lblRelatedGender3.Text = loadRow.RelatedGender3;
            this.lblRelatedAge3.Text = loadRow.RelatedAge3;
            this.lblRelatedCompany3.Text = loadRow.RelatedCompany3;
            this.lblRelatedPosition3.Text = loadRow.RelatedPosition3;

            this.lblApprovalComments.Text = loadRow.ApprovalComments;
        }

        private void SwitchControls(bool isLabelsShow, bool isControlsShow)
        {
            //labels
            this.lblFillerCompany.Visible = isLabelsShow;
            this.lblFillDate.Visible = isLabelsShow;
            this.lblFiller.Visible = isLabelsShow;
            this.lblGender.Visible = isLabelsShow;
            this.lblAge.Visible = isLabelsShow;
            this.lblCompany.Visible = isLabelsShow;
            this.lblPosition.Visible = isLabelsShow;
            this.lblRank.Visible = isLabelsShow;

            // relation1
            this.lblRelatedName1.Visible = isLabelsShow;
            this.lblRelationship1.Visible = isLabelsShow;
            this.lblRelatedGender1.Visible = isLabelsShow;
            this.lblRelatedAge1.Visible = isLabelsShow;
            this.lblRelatedCompany1.Visible = isLabelsShow;
            this.lblRelatedPosition1.Visible = isLabelsShow;
            //relation2
            this.lblRelatedName2.Visible = isLabelsShow;
            this.lblRelationship2.Visible = isLabelsShow;
            this.lblRelatedGender2.Visible = isLabelsShow;
            this.lblRelatedAge2.Visible = isLabelsShow;
            this.lblRelatedCompany2.Visible = isLabelsShow;
            this.lblRelatedPosition2.Visible = isLabelsShow;
            //relation3
            this.lblRelatedName3.Visible = isLabelsShow;
            this.lblRelationship3.Visible = isLabelsShow;
            this.lblRelatedGender3.Visible = isLabelsShow;
            this.lblRelatedAge3.Visible = isLabelsShow;
            this.lblRelatedCompany3.Visible = isLabelsShow;
            this.lblRelatedPosition3.Visible = isLabelsShow;

            this.lblApprovalComments.Visible = isLabelsShow;

            //controls
            this.txtFillerCompany.Visible = isControlsShow;
            this.dtFillDate.Visible = isControlsShow;
            this.txtFiller.Visible = isControlsShow;
            this.rbGender.Visible = isControlsShow;
            this.txtAge.Visible = isControlsShow;
            this.txtCompany.Visible = isControlsShow;
            this.txtPosition.Visible = isControlsShow;
            this.txtRank.Visible = isControlsShow;

            // relation1
            this.txtRelatedName1.Visible = isControlsShow;
            this.txtRelationship1.Visible = isControlsShow;
            this.rbRelatedGender1.Visible = isControlsShow;
            this.txtRelatedAge1.Visible = isControlsShow;
            this.txtRelatedCompany1.Visible = isControlsShow;
            this.txtRelatedPosition1.Visible = isControlsShow;
            //relation2
            this.txtRelatedName2.Visible = isControlsShow;
            this.txtRelationship2.Visible = isControlsShow;
            this.rbRelatedGender2.Visible = isControlsShow;
            this.txtRelatedAge2.Visible = isControlsShow;
            this.txtRelatedCompany2.Visible = isControlsShow;
            this.txtRelatedPosition2.Visible = isControlsShow;
            //relation3
            this.txtRelatedName3.Visible = isControlsShow;
            this.txtRelationship3.Visible = isControlsShow;
            this.rbRelatedGender3.Visible = isControlsShow;
            this.txtRelatedAge3.Visible = isControlsShow;
            this.txtRelatedCompany3.Visible = isControlsShow;
            this.txtRelatedPosition3.Visible = isControlsShow;

            this.txtApprovalComments.Visible = isControlsShow;
            this.hr6.Visible = isControlsShow;
            this.tbSubmit.Visible = isControlsShow;
        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
            using (IncorruptProxy proxy = new IncorruptProxy())
            {
                if (Session["EditJianChaGanbuPeiOuCongYeDT"] != null)
                {
                    var editDT = (IncorruptDataSet.T_JianChaGanbuPeiOuCongYeDataTable)Session["EditJianChaGanbuPeiOuCongYeDT"];
                    var editRow = editDT[0];

                    editRow.FillerCompany = this.txtFillerCompany.Text;
                    editRow.FillDate = this.dtFillDate.SelectedDate;
                    editRow.Filler = this.txtFiller.Text;
                    editRow.Gender = this.rbGender.SelectedValue;
                    editRow.Age = this.txtAge.Text;
                    editRow.Company_Dept = this.txtCompany.Text;
                    editRow.Position = this.txtPosition.Text;
                    editRow.Rank = this.txtRank.Text;

                    // relation1
                    editRow.RelatedName1 = this.txtRelatedName1.Text;
                    editRow.Relationship1 = this.txtRelationship1.Text;
                    editRow.RelatedGender1 = this.rbRelatedGender1.SelectedValue;
                    editRow.RelatedAge1 = this.txtRelatedAge1.Text;
                    editRow.RelatedCompany1 = this.txtRelatedCompany1.Text;
                    editRow.RelatedPosition1 = this.txtRelatedPosition1.Text;
                    //relation2
                    editRow.RelatedName2 = this.txtRelatedName2.Text;
                    editRow.Relationship2 = this.txtRelationship2.Text;
                    editRow.RelatedGender2 = this.rbRelatedGender2.SelectedValue;
                    editRow.RelatedAge2 = this.txtRelatedAge2.Text;
                    editRow.RelatedCompany2 = this.txtRelatedCompany2.Text;
                    editRow.RelatedPosition2 = this.txtRelatedPosition2.Text;
                    //relation3
                    editRow.RelatedName3 = this.txtRelatedName3.Text;
                    editRow.Relationship3 = this.txtRelationship3.Text;
                    editRow.RelatedGender3 = this.rbRelatedGender3.SelectedValue;
                    editRow.RelatedAge3 = this.txtRelatedAge3.Text;
                    editRow.RelatedCompany3 = this.txtRelatedCompany3.Text;
                    editRow.RelatedPosition3 = this.txtRelatedPosition3.Text;

                    editRow.ApprovalComments = this.txtApprovalComments.Text;

                    JCYUtility.UpdateCommonFields(editRow, "Iori");
                    proxy.UpdateJianChaGanbuPeiOuCongYe(editDT);

                    Response.Redirect("JianChaGanbuPeiOuCongYe.aspx");
                }
            }
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("JianChaGanbuPeiOuCongYe.aspx");
        }

        protected void lkbEditJianChaGanbuPeiOuCongYe_Click(object sender, EventArgs e)
        {
            this.hfIsEdit.Value = "1";
            SwitchControls(false, true);
            if (_isApproval == "1")
                this.txtApprovalComments.Enabled = true;
            else
                this.txtApprovalComments.Enabled = false;

            this.dtFillDate.SelectedDate = Convert.ToDateTime(this.hfFillDate.Value);
        }
    }
}
